import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*

def entity
def query
PreparedQuery pq

if(params.title){
	
	query = new Query("article")

	query.addFilter("title", Query.FilterOperator.EQUAL, params.title) 
	
	pq = datastore.prepare(query)
	
	entity = pq.asSingleEntity()
	
}else{
	
	def key = KeyFactory.createKey("article",Long.parseLong(params.id))
	
	entity = datastore.get(key)
}

query = new Query("comment")

query.addFilter("aId", Query.FilterOperator.EQUAL, (entity.key.id).toString())

query.addSort("pubTime", Query.SortDirection.DESCENDING)

pq = datastore.prepare(query)

request.setAttribute 'blog', entity

request.setAttribute 'comments', pq.asList(withLimit(10))

forward "singleBlog.gtpl"